﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;



namespace WindowsGame
{
	public delegate void TextUpdateEvent(Text sender, uint id);


	public class Menu
	{
		Texture2D background = null;
		List<Text> texts;
		List<Button> buttons;
		List<Selector> selectors;


		public event TextUpdateEvent TextUpdate;



		public Menu()
		{
			texts = new List<Text>();
			buttons = new List<Button>();
			selectors = new List<Selector>();
		}



		public Menu(Texture2D backgroundIn)
		{
			background = backgroundIn;

			texts = new List<Text>();
			buttons = new List<Button>();
			selectors = new List<Selector>();
		}



		public Button AddButton(Vector2 pos, Texture2D caption, Texture2D rollOver)
		{
			Button button = new Button(caption, rollOver, pos);
			buttons.Add(button);

			return button;
		}



		public void AddButton(Button button)
		{
			buttons.Add(button);
		}



		public void AddText(Vector2 pos, Texture2D caption)
		{
			Text text = new Text(caption, pos);
			texts.Add(text);
		}



		public void AddText(Vector2 pos, string caption, Color color, SpriteFont spriteFont)
		{
			Text text = new Text(caption, color, pos, spriteFont);
			texts.Add(text);
		}



		public void AddText(Text text)
		{
			texts.Add(text);
		}



		public void AddSelector(Selector selector)
		{
			selectors.Add(selector);
		}



		public Selector AddSelector(Vector2 pos, Texture2D plus, Texture2D plusRollOver, Texture2D minus, Texture2D minusRollOver,
									Texture2D captionIn, Texture2D selectedIn, int resolutionWidth)
		{
			Selector selector = new Selector(plus, plusRollOver, minus, minusRollOver, captionIn, selectedIn, pos, resolutionWidth);
			selectors.Add(selector);
			return selector;
		}



		public Selector AddSelector(Vector2 pos, Texture2D plus, Texture2D plusRollOver, Texture2D minus, Texture2D minusRollOver,
							Texture2D captionIn, string selectedIn, Color selectedColor, SpriteFont selectedSpriteFont, int resolutionWidth)
		{
			Selector selector = new Selector(plus, plusRollOver, minus, minusRollOver, captionIn, selectedIn,
				selectedColor, selectedSpriteFont, pos, resolutionWidth);
			selectors.Add(selector);
			return selector;
		}



		public void Update(GameTime gameTime)
		{
			foreach(Button b in buttons)
				b.Update(gameTime);
			uint id = 0;
			foreach(Text t in texts)
			{
				t.Update(gameTime);
				if(TextUpdate != null)
					TextUpdate(t, id++);
			}
			foreach(Selector s in selectors)
				s.Update(gameTime);
		}



		public void Draw(SpriteBatch spriteBatch, GameTime gameTime, float scale)
		{
			if(background != null)
				spriteBatch.Draw(background, Vector2.Zero, null, Color.White, 0.0f, Vector2.Zero, scale, SpriteEffects.None, 0.0f);

			foreach(Button b in buttons)
				b.Draw(spriteBatch, gameTime, scale);
			foreach(Text t in texts)
				t.Draw(spriteBatch, gameTime, scale);
			foreach(Selector s in selectors)
				s.Draw(spriteBatch, gameTime, scale);
		}
	}
}
